package com.lanlian.cayman;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.multidex.MultiDex;
import android.support.v7.app.AppCompatDelegate;
import android.webkit.WebView;

import com.lanlian.cayman.rabbitmq.MQFactory;
import com.lanlian.commonlib.BaseApplication;
import com.lanlian.commonlib.bean.PositionDetailsInfo;
import com.lanlian.commonlib.bean.ReqUpdateUserRegister;
import com.lanlian.commonlib.bean.ReqUserRegister;
import com.lanlian.commonlib.bean.UnitAssetInfo;
import com.lanlian.commonlib.common.RabbitMqConfig;
import com.lanlian.commonlib.common.SystemConfig;
import com.lanlian.commonlib.dao.DBManager;
import com.lanlian.commonlib.util.CrashHandler;
import com.lanlian.utillib.ImageUtils;
import com.lanlian.volleylib.HttpRequestManager;
import com.scwang.smartrefresh.header.WaterDropHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.umeng.commonsdk.UMConfigure;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : xiaobo.hu@foxtry.com
 *     date   : 2018/8/9 13:40
 *     desc   : 主程序Application
 *     version: 1.0
 * </pre>
 */
public class LLApplication extends BaseApplication {

    private static LLApplication instance;

    public static Double deposit2;//计算动态权益保证金
    public static Double deposit;//保证金
    public static Double overnightDeposit;//隔夜保证金
    public static Double positionPnl;//持仓盈亏
    public static Double actualPnl;//实际盈亏
    public static Double availableFunds;//可用资金
    public static Double dynamicPnl;//动态权益
    public static Double beginCash;//昨结存
    public static Double intoCash;//今日入金
    public static Double withdrawCash;//今日出金
    public static Double drawAble;//可提现金额

    public static UnitAssetInfo mUnitAssetInfo = new UnitAssetInfo();//资金信息
    public static ArrayList<PositionDetailsInfo> positionList = new ArrayList<>();//持仓列表
    public static ReqUserRegister reqUserRegister;
    public static ReqUpdateUserRegister reqUpdateUserRegister;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        HttpRequestManager.getInstance().init(this);
        ImageUtils.init(this);
        DBManager.getInstance().init(this);
        if (DBManager.getInstance().getTopicColor() == 1) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        } else if (DBManager.getInstance().getTopicColor() == 2) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }
        setRefreshLayoutHeader();
        RabbitMqConfig rabbitMqConfig = SystemConfig.getMQConfig();
        MQFactory.getInstance().init(rabbitMqConfig.getIp(), rabbitMqConfig.getPort(),
                rabbitMqConfig.getUser(), rabbitMqConfig.getPassword());
        initCrashHandler();
        UMConfigure.init(this, "5bbb1a20f1f5560dc7000125", "Android", UMConfigure.DEVICE_TYPE_PHONE, "");
        UMConfigure.setLogEnabled(true);
        initWebView();
    }

    private void initWebView() {
        new WebView(this).destroy();
    }

    private void setRefreshLayoutHeader() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.color_K4, R.color.color_B1);
                return new WaterDropHeader(context);
            }
        });
    }

    public static LLApplication getInstance() {
        return instance;
    }

    public static Double getPositionPnl() {
        return positionPnl == null ? 0 : positionPnl;
    }

    public static Double getDeposit() {
        return deposit == null ? 0 : deposit;
    }

    public static Double getDeposit2() {
        return deposit2 == null ? 0 : deposit2;
    }

    public static Double getActualPnl() {
        return actualPnl == null ? 0 : actualPnl;
    }

    public static Double getAvailableFunds() {
        return availableFunds == null ? 0 : availableFunds;
    }

    public static Double getOvernightDeposit() {
        return overnightDeposit == null ? 0 : overnightDeposit;
    }

    public static Double getDynamicPnl() {
        return dynamicPnl == null ? 0 : dynamicPnl;
    }

    public static Double getBeginCash() {
        return beginCash == null ? 0 : beginCash;
    }

    public static Double getIntoCash() {
        return intoCash == null ? 0 : intoCash;
    }

    public static Double getWithdrawCash() {
        return withdrawCash == null ? 0 : withdrawCash;
    }

    public static Double getDrawAble() {
        return drawAble == null ? 0 : drawAble;
    }

    public static UnitAssetInfo getUnitAssetInfo() {
        return mUnitAssetInfo;
    }

    public static ArrayList<PositionDetailsInfo> getPositionList() {
        return positionList;
    }

    public static void setPositionList(ArrayList<PositionDetailsInfo> positionList) {
        LLApplication.positionList = positionList;
    }

    private void initCrashHandler() {
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

}
